package com.lyc.campuslife.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyc.campuslife.dto.Result;
import com.lyc.campuslife.dto.UserDTO;
import com.lyc.campuslife.entity.Blog;
import com.lyc.campuslife.service.IBlogService;
import com.lyc.campuslife.utils.SystemConstants;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.lyc.campuslife.utils.HttpRequestConstants.USER_INFO;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lyc
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource
    private IBlogService blogService;

    //发布博客
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog, HttpServletRequest request) {
        UserDTO userDTO = (UserDTO)request.getAttribute(USER_INFO);
        Long userId = userDTO.getId();
        blog.setUserId(userId);
        return blogService.saveBlog(blog);
    }

    //点赞博客
    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id,HttpServletRequest request) {
        UserDTO userDTO = (UserDTO)request.getAttribute(USER_INFO);
        Long userId = userDTO.getId();
        return blogService.likeBlog(id,userId);
    }

    //查询博客的前五名点赞用户
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id) {
        return blogService.queryBlogLikes(id);
    }

    //查询主页热门博客数组
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current,HttpServletRequest request) {
        Long userId = null;
        if(request.getAttribute(USER_INFO)!=null){
            UserDTO userDTO = (UserDTO)request.getAttribute(USER_INFO);
            userId = userDTO.getId();
        }
        return blogService.queryHotBlog(current,userId);
    }

    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(
            @RequestParam("lastId") Long max, @RequestParam(value = "offset", defaultValue = "0") Integer offset,HttpServletRequest request) {
        UserDTO userDTO = (UserDTO)request.getAttribute(USER_INFO);
        Long userId = userDTO.getId();
        return blogService.queryBlogOfFollow(max, offset,userId);
    }

    //TODO 下面三个接口有待优化
    //根据博客id查博客
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id,HttpServletRequest request) {
        Long userId = null;
        if(request.getAttribute(USER_INFO)!=null){
            UserDTO userDTO = (UserDTO)request.getAttribute(USER_INFO);
            userId = userDTO.getId();
        }
        return blogService.queryBlogById(id,userId);
    }

    //查询我的博客数组
    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current,HttpServletRequest request) {
        UserDTO userDTO = (UserDTO)request.getAttribute(USER_INFO);
        Long userId = userDTO.getId();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", userId).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    //根据用户id查博客数组
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }


}
